package com.util;

import java.util.HashMap;
import java.util.Map;

import javax.servlet.http.HttpServletResponse;

import com.google.gson.Gson;

public class ReturnValue {
	/**
	 * 返回给页面JSON对象
	 * @param response 
	 * @param obj 要返回的JSON
	 * @throws Exception
	 */
	public static void getResponse (HttpServletResponse response,Object obj) throws Exception{
		Gson gson=new Gson();
		String result =gson.toJson(obj);
		response.setCharacterEncoding("UTF-8");
		response.setContentType("text/html;charset=UTF-8");
        response.setHeader("Cache-Control","no-store, max-age=0, no-cache, must-revalidate");    
        response.addHeader("Cache-Control", "post-check=0, pre-check=0");
        response.setHeader("Pragma", "no-cache");
        System.out.println(result);
        response.getWriter().print(result);
	}
	/**
	 * 返回给页面Success或Fail   
	 * val （true 返回 Success  false 返回 Fail）
	 * @param response
	 * @param val 布尔值 
	 * @throws Exception
	 */
	public static void returnBySuccessOrFail(HttpServletResponse response,boolean val,String msg) throws Exception{
		Map<String, String> map = new HashMap<String, String>();
		if(val)
			map.put("code", "Success");
		else
			map.put("code", "Fail");
		map.put("msg", msg);
		Gson gson=new Gson();
		String result =gson.toJson(map);
		response.setCharacterEncoding("UTF-8");
		response.setContentType("text/html;charset=UTF-8");
        response.setHeader("Cache-Control","no-store, max-age=0, no-cache, must-revalidate");    
        response.addHeader("Cache-Control", "post-check=0, pre-check=0");
        response.setHeader("Pragma", "no-cache");
        System.out.println(result);
        response.getWriter().print(result);
	}
}
